using System;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Collections.Generic;
using System.Reflection;

namespace DocumentManagement
{
  /// <summary>
  /// Tlettertemplate Data Object Class
  /// </summary>
  public partial class DOTlettertemplate
  {
    #region Class Constant declarations
    		/// <summary>
		/// The Table Column for the Tlettertemplate lettertemplateid
		/// </summary>
		public const string Rowlettertemplateid = "lettertemplateid";

		/// <summary>
		/// The Table Column for the Tlettertemplate lettertemplatename
		/// </summary>
		public const string Rowlettertemplatename = "lettertemplatename";

		/// <summary>
		/// The Table Column for the Tlettertemplate lettertemplatetype
		/// </summary>
		public const string Rowlettertemplatetype = "lettertemplatetype";

		/// <summary>
		/// The Table Column for the Tlettertemplate lettertemplatedescription
		/// </summary>
		public const string Rowlettertemplatedescription = "lettertemplatedescription";

		/// <summary>
		/// The Table Column for the Tlettertemplate active
		/// </summary>
		public const string Rowactive = "active";


#region Stored Procedures
private const string m_StoredProcedureConstCreate = "spTlettertemplate_Create";
private const string m_StoredProcedureConstDelete = "spTlettertemplate_Delete";
private const string m_StoredProcedureConstRetrieve = "spTlettertemplate_Retrieve";
private const string m_StoredProcedureConstRetrieveall = "spTlettertemplate_Retrieveall";
private const string m_StoredProcedureConstUpdate = "spTlettertemplate_Update";
#endregion
    
    
    #endregion

    #region Class Variable declarations
    	#region DO Variables
		private int m_lettertemplateid;
		private string m_lettertemplatename;
		private string m_lettertemplatetype;
		private string m_lettertemplatedescription;
		private bool m_active;
	#endregion
    
	#region Default Variables
	private SQLDataObject sqlData;
	private List<DOTlettertemplate> m_Tlettertemplates = new List<DOTlettertemplate>();
	#endregion
	
    #endregion

    #region Class Constructors & Destructors
    /// <summary>
    /// Class Constructor
    /// </summary>
    public DOTlettertemplate() { }
    #endregion

    #region Class Properties
    #region DO Properties
		/// <summary>
		/// lettertemplateid
		/// </summary>
		/// <remarks>
		/// This property contains the Tlettertemplate lettertemplateid
		/// </remarks>
		public int lettertemplateid
		{
			get { return (this.m_lettertemplateid); }
			set { this.m_lettertemplateid = value; }
		}

		/// <summary>
		/// lettertemplatename
		/// </summary>
		/// <remarks>
		/// This property contains the Tlettertemplate lettertemplatename
		/// </remarks>
		public string lettertemplatename
		{
			get { return (this.m_lettertemplatename); }
			set { this.m_lettertemplatename = value; }
		}

		/// <summary>
		/// lettertemplatetype
		/// </summary>
		/// <remarks>
		/// This property contains the Tlettertemplate lettertemplatetype
		/// </remarks>
		public string lettertemplatetype
		{
			get { return (this.m_lettertemplatetype); }
			set { this.m_lettertemplatetype = value; }
		}

		/// <summary>
		/// lettertemplatedescription
		/// </summary>
		/// <remarks>
		/// This property contains the Tlettertemplate lettertemplatedescription
		/// </remarks>
		public string lettertemplatedescription
		{
			get { return (this.m_lettertemplatedescription); }
			set { this.m_lettertemplatedescription = value; }
		}

		/// <summary>
		/// active
		/// </summary>
		/// <remarks>
		/// This property contains the Tlettertemplate active
		/// </remarks>
		public bool active
		{
			get { return (this.m_active); }
			set { this.m_active = value; }
		}
		#endregion
    
	#region Default Properties
	public List<DOTlettertemplate> Tlettertemplates
	{
	  get { return m_Tlettertemplates; }
	  set { m_Tlettertemplates = value; }
	}    
	#endregion
	
    #endregion

    #region Class Methods
    public DataSet Retrieve()
    {
      DataSet returnDS = new DataSet();
      sqlData = new SQLDataObject();
      sqlData.ExecuteDataSetSQL(m_StoredProcedureConstRetrieve + " @" + Rowlettertemplateid + "=" + this.lettertemplateid);
      return returnDS;
    }

    public bool RetrieveAll()
    {
      sqlData = new SQLDataObject();
      DataSet resultDS = sqlData.ExecuteDataSetSQL(m_StoredProcedureConstRetrieveall);
      return SetClassPropertiesListFromDataSet(resultDS);
    }

    public bool Create()
    {
      bool success = false;
      string sVariables = "";
      sqlData = new SQLDataObject();

      Type type = this.GetType();
      PropertyInfo[] propInfo = type.GetProperties();

      for (int i = 0; i < propInfo.Length; i++)
      {
        if (propInfo[i].PropertyType.Name.IndexOf("List") == -1)
        {
          sVariables += "@" + propInfo[i].Name + " = ";
          if (propInfo[i].PropertyType.Name == "String")
            sVariables += "'" + propInfo[i].GetValue(this, null) + "', ";
          else
            sVariables += propInfo[i].GetValue(this, null) + ", ";
        }
      }

      success = sqlData.ExecuteNonQuerySQL(m_StoredProcedureConstCreate + " " + sVariables.Substring(0, sVariables.Length - 2));

      return success;
    }

    public bool Update()
    {
      bool success = false;
      string sVariables = "";
      sqlData = new SQLDataObject();

      Type type = this.GetType();
      PropertyInfo[] propInfo = type.GetProperties();

      for (int i = 0; i < propInfo.Length; i++)
      {
        if (propInfo[i].PropertyType.Name.IndexOf("List") == -1)
        {
          sVariables += "@" + propInfo[i].Name + " = ";
          if (propInfo[i].PropertyType.Name == "String")
            sVariables += "'" + propInfo[i].GetValue(this, null) + "', ";
          else
            sVariables += propInfo[i].GetValue(this, null) + ", ";
        }
      }

      success = sqlData.ExecuteNonQuerySQL(m_StoredProcedureConstUpdate + " " + sVariables.Substring(0, sVariables.Length - 2));

      return success;
    }

    private bool SetClassPropertiesFromDataSet(DataSet resultDS)
    {
      bool success = false;
      if (resultDS.Tables[0] != null)
      {
        if (resultDS.Tables[0].Rows.Count > 0)
        {
          foreach (DataRow dr in resultDS.Tables[0].Rows)
          {
					if (dr[Rowlettertemplateid].ToString() != "")
			this.lettertemplateid = int.Parse(dr[Rowlettertemplateid].ToString());

		if (dr[Rowlettertemplatename].ToString() != "")
			this.lettertemplatename = dr[Rowlettertemplatename].ToString();

		if (dr[Rowlettertemplatetype].ToString() != "")
			this.lettertemplatetype = dr[Rowlettertemplatetype].ToString();

		if (dr[Rowlettertemplatedescription].ToString() != "")
			this.lettertemplatedescription = dr[Rowlettertemplatedescription].ToString();

		if (dr[Rowactive].ToString() != "")
			this.active = bool.Parse(dr[Rowactive].ToString());
          }
          success = true;
        }
      }
      return success;
    }

    private bool SetClassPropertiesListFromDataSet(DataSet resultDS)
    {
      bool success = false;
      if (resultDS.Tables[0] != null)
      {
        if (resultDS.Tables[0].Rows.Count > 0)
        {
          foreach (DataRow dr in resultDS.Tables[0].Rows)
          {
            DOTlettertemplate Tlettertemplate = new DOTlettertemplate();
			
					if (dr[Rowlettertemplateid].ToString() != "")
			Tlettertemplate.lettertemplateid = int.Parse(dr[Rowlettertemplateid].ToString());

		if (dr[Rowlettertemplatename].ToString() != "")
			Tlettertemplate.lettertemplatename = dr[Rowlettertemplatename].ToString();

		if (dr[Rowlettertemplatetype].ToString() != "")
			Tlettertemplate.lettertemplatetype = dr[Rowlettertemplatetype].ToString();

		if (dr[Rowlettertemplatedescription].ToString() != "")
			Tlettertemplate.lettertemplatedescription = dr[Rowlettertemplatedescription].ToString();

		if (dr[Rowactive].ToString() != "")
			Tlettertemplate.active = bool.Parse(dr[Rowactive].ToString());

            this.Tlettertemplates.Add(Tlettertemplate);
          }
          success = true;
        }
      }
      return success;
    }
    #endregion

    #region Class Support Methods
    #endregion
  }
}
